#include "StdAfx.h"
#include "Shotgun.h"
#include "EntityManager.h"
#include "StateFactory.h"

#include <math.h>

const float PI = 3.14159265358979323846f;
#define random()	((rand () & 0x7fff) / ((float)0x7fff))
#define crandom()	(2.0f * (random() - 0.5f))

Shotgun::Shotgun(void) : IWeapon()
{
    m_attributes.Put( "bullets/sec", 30 );
    m_attributes.Put( "spread", 222.4f );
    m_attributes.Put( "distance", 300 );
    m_attributes.Put( "weapontime", 800.0f );
    m_attributes.Put( "damage", 15 );

    m_attributes.Put("reloadtime", 200.0f );
    m_attributes.Put("ammobin", 30 );
    m_attributes.Put("reload", 1);
    m_attributes.Put("clip", 5);
    m_attributes.Put("clipmax", 5);
  

}

Shotgun::~Shotgun(void)
{

}


/** Fire this weapon */
void Shotgun::Fire()
{
    IWeapon::Fire();
    if ( IsFiring() )
    {   // report stats:
        //std::cout   << "Ammo bin: " << m_attributes.GetInt("ammobin") << "\n"
        //            << "Clip: " << m_attributes.GetInt("clip") << "\n"
        //            ;
        if ( ExpenseRound() )
        {
            SpreadBullets( m_owner->m_forward, m_owner->m_physObject.GetPosition() );
        }
    }

}

/** Randomize bullets */
void Shotgun::SpreadBullets(const Vector2f &forward, const Vector2f &start )
{
    int amount = m_attributes.GetInt("bullets/sec");
    int distance = m_attributes.GetInt( "distance" );
    float spread = m_attributes.GetFloat("spread");

    Vector2f startOffset( 32, 32 );
    startOffset = startOffset + start;

    std::vector< IEntity* > list;
    for ( int i = 0; i < amount; ++i )
    {
        Vector2f end( sin( crandom() )*spread, sin( crandom() )*spread );
        end = end + ( forward * distance );

        IEntity* ent = EntityManager::GetInstance()->CheckLineHit( startOffset, startOffset + end );
        if ( ent )
        {
            CollideWith( ent );
        }
    }

    // Create Particle Effects for the gun

}

/** Expense a Round, if we can */
bool Shotgun::ExpenseRound()
{
    int amt = m_attributes.GetInt( "clip" );
    bool hasRound = amt > 0;

    if ( hasRound )
        m_attributes.SetInt( "clip", --amt );

    return hasRound;
}
/** Reload this weapon */
//void Shotgun::Reload()
//{
//
//}

/** Collide with this weapon */
//bool Shotgun::CollideWith( IEntity *ent )
//{
//    return false;
//}